<template>
  <div class="app-container">
    <h2 class="page-title">{{ isEdit ? '编辑轮播图' : '添加轮播图' }}</h2>
    <el-form label-width="100px" class="custom-form">
      <el-form-item label="标题" class="form-item">
        <el-input v-model="banner.title" placeholder="请输入标题" clearable />
      </el-form-item>

      <el-form-item label="链接地址" class="form-item">
        <el-input v-model="banner.linkUrl" placeholder="请输入链接地址" clearable />
      </el-form-item>

      <el-form-item label="图片" class="form-item image-upload-item">
        <div class="image-upload-wrapper" @click="triggerFileInput" role="button" tabindex="0">
          <input
            ref="fileInput"
            type="file"
            accept="image/*"
            @change="handleFileChange"
            style="display: none"
          />
          <template v-if="banner.imageUrl">
            <img :src="banner.imageUrl" alt="上传图片" class="uploaded-image" />
          </template>
          <template v-else>
            <div class="placeholder">
              <i class="el-icon-plus" />
              <span class="placeholder-text">点击上传图片</span>
            </div>
          </template>
        </div>
      </el-form-item>

      <el-form-item class="form-item btn-item">
        <el-button
          type="primary"
          :disabled="saveBtnDisabled"
          class="submit-btn"
          @click="submitForm"
        >
          {{ isEdit ? '修改' : '保存' }}
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import bannerApi from '@/api/edu/banner'

export default {
  data() {
    return {
      banner: {
        title: '',
        imageUrl: '',
        linkUrl: ''
      },
      saveBtnDisabled: false
    }
  },
  computed: {
    isEdit() {
      return !!this.banner.id
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      const id = this.$route.params.id
      if (id) {
        bannerApi.getById(id).then(res => {
          if (res.code === 20000) {
            const { id, title, imageUrl, linkUrl } = res.data
            this.banner = { id, title, imageUrl, linkUrl }
          }
        })
      }
    },
    triggerFileInput() {
      this.$refs.fileInput.click()
    },
    // handleFileChange(event) {
    //   const file = event.target.files[0]
    //   if (!file) return
    //
    //   const formData = new FormData()
    //   formData.append('file', file)
    //
    //   const uploadUrl = process.env.VUE_APP_BASE_API + 'eduoss/upload/avatar'
    //
    //   fetch(uploadUrl, {
    //     method: 'POST',
    //     body: formData
    //   })
    //     .then(res => res.json())
    //     .then(data => {
    //       if (data && data.url) {
    //         this.banner.imageUrl = data.url
    //         this.$message.success('上传成功')
    //       } else {
    //         this.$message.error('上传失败')
    //       }
    //     })
    //     .catch(() => {
    //       this.$message.error('上传失败')
    //     })
    //     .finally(() => {
    //       event.target.value = ''
    //     })
    // },
    handleFileChange(event) {
      const file = event.target.files[0]
      if (!file) return

      const formData = new FormData()
      formData.append('file', file)

      const uploadUrl = process.env.VUE_APP_BASE_API + 'admin/oss/upload'

      fetch(uploadUrl, {
        method: 'POST',
        body: formData
      })
        .then(res => res.json())
        .then(data => {
          if (data && data.code === 20000 && data.data) {
            this.banner.imageUrl = data.data
            this.$message.success('上传成功')
          } else {
            this.$message.error('上传失败')
          }
        })
        .catch(() => {
          this.$message.error('上传失败')
        })
        .finally(() => {
          event.target.value = ''
        })
    },
    submitForm() {
      this.saveBtnDisabled = true
      const api = this.isEdit ? bannerApi.updateBanner : bannerApi.save

      api(this.banner)
        .then(res => {
          if (res.code === 20000) {
            this.$message.success(this.isEdit ? '修改成功！' : '添加成功！')
            this.$router.push({ path: '/banner/table' })
          } else {
            this.$message.error(res.message || '操作失败')
          }
        })
        .catch(() => {
          this.$message.error('请求失败')
        })
        .finally(() => {
          this.saveBtnDisabled = false
        })
    }
  }
}
</script>

<style scoped>
.app-container {
  max-width: 720px;
  margin: 40px auto;
  padding: 40px 50px 60px;
  background: linear-gradient(145deg, #ffffff 0%, #f8f9fa 100%);
  border-radius: 24px;
  box-shadow: 0 16px 48px rgba(50, 50, 93, 0.08), 0 6px 20px rgba(0, 0, 0, 0.05);
  position: relative;
  overflow: hidden;
  font-family: 'Segoe UI', -apple-system, BlinkMacSystemFont, sans-serif;
}

.app-container::before {
  content: '';
  position: absolute;
  top: -80px;
  right: -80px;
  width: 200px;
  height: 200px;
  background: rgba(64, 158, 255, 0.08);
  border-radius: 50%;
  z-index: 1;
}

.page-title {
  font-size: 32px;
  font-weight: 800;
  text-align: center;
  margin-bottom: 48px;
  color: #2d3748;
  position: relative;
  z-index: 2;
  background: -webkit-linear-gradient(45deg, #409eff, #63b3ed);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  text-shadow: 0 2px 4px rgba(64, 158, 255, 0.1);
}

.custom-form {
  position: relative;
  z-index: 2;
}

.form-item {
  margin-bottom: 32px;
}

.form-item label {
  font-weight: 600;
  font-size: 16px;
  color: #4a5568;
  display: block;
  margin-bottom: 8px;
}

.el-input__inner {
  height: 48px;
  padding: 0 20px;
  font-size: 16px;
  border-radius: 12px;
  background: #f7fafc;
  border: 2px solid #e2e8f0;
  transition: all 0.3s ease;
}

.el-input__inner:focus {
  border-color: #409eff;
  box-shadow: 0 0 0 3px rgba(64, 158, 255, 0.1);
  background: white;
}

.image-upload-item {
  margin-top: 12px;
}

.image-upload-wrapper {
  width: 100%;
  height: 240px;
  border: 2px dashed #cbd5e0;
  border-radius: 16px;
  background: #f7fafc;
  position: relative;
  transition: all 0.3s ease;
  overflow: hidden;
  cursor: pointer;
}

.image-upload-wrapper:hover {
  border-color: #409eff;
  background: #f8fafc;
}

.placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: #a0aec0;
  gap: 8px;
  transition: all 0.3s ease;
  user-select: none;
  cursor: pointer;
}

.placeholder:hover {
  color: #409eff;
}

.el-icon-plus {
  font-size: 48px;
  transition: all 0.3s ease;
}

.uploaded-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 12px;
  transition: all 0.3s ease;
}

.uploaded-image:hover {
  transform: scale(1.02);
}

.btn-item {
  margin-top: 56px;
  text-align: center;
}

.submit-btn {
  min-width: 200px;
  height: 56px;
  font-size: 18px;
  font-weight: 600;
  border-radius: 14px;
  background: linear-gradient(135deg, #409eff 0%, #63b3ed 100%);
  border: none;
  position: relative;
  overflow: hidden;
  transition: all 0.3s ease;
}

.submit-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 10px 20px rgba(64, 158, 255, 0.2);
}

.submit-btn:active {
  transform: translateY(0);
}

.submit-btn:disabled {
  background: #cbd5e0 !important;
  cursor: not-allowed;
  opacity: 0.7;
}

/* 自定义滚动条 */
::-webkit-scrollbar {
  width: 8px;
}

::-webkit-scrollbar-track {
  background: #f7fafc;
  border-radius: 4px;
}

::-webkit-scrollbar-thumb {
  background: #cbd5e0;
  border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
  background: #a0aec0;
}
</style>
